#include "main.h"
#include "cmsis_os2.h"
#include <sys/time.h>
#include "xtsAdd.h"
#include <stdio.h>
#include "sys/times.h"

/**
 * @brief 延时函数
 * 
 * @param value 延时单位ms
 */
void sleep(int value)
{
    osDelay(value * 1000);//使用CMSIS中 osDelay的实现
}

/**
 * @brief 使能硬件看门狗
 * 
 */
void IoTWatchDogEnable(void)
{
}

/**
 * @brief 看门狗喂狗
 * 在xts部分线程中会循环执行较长时间，会调用当前
 * 函数喂狗，防止硬件复位
 */
void IoTWatchDogKick(void)
{
}

/**
 * @brief IoTWatchDogDisable 禁止看门狗
 * 
 */
void IoTWatchDogDisable(void)
{
}

/**
 * @brief 创建互斥锁
 * 
 * @param mutex 
 * @param mutexAttr 
 * @return int 
 */
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)
{
    return 0;
}

/**
 * @brief 上锁
 * 
 * @param mutex 
 * @return int 
 */
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
    return 1;
}

/**
 * @brief 解锁
 * 
 * @param mutex 
 * @return int 
 */
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
    return 22;
}

/**
 * @brief 进入临界区
 * 
 * @return uint32_t 保存状态寄存器
 */
uint32_t ArchIntLock(void)
{
    return osKernelLock();//使用CMSIS中的实现
}

/**
 * @brief 恢复现场
 * 
 * @param intSave 恢复状态寄存器
 */
void ArchIntRestore(uint32_t intSave)
{
    osKernelRestoreLock(intSave);//使用CMSIS中的实现
}

/**
 * @brief 得到当前时钟
 * 
 * @param clock_id 
 * @param tp 
 * @return int 
 */
int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
    uint32_t tick = osKernelGetTickCount();
    tp->tv_sec = tick / 1000;
    tp->tv_nsec = tick;
    return 0;
}

/**
 * @brief 得到当前任务的消息队列ID
 * 消息队列ID在创建任务时,由第二个参数带入
 * @return void* 
 */
extern void *OnGetCurrentTcbUserPara(void);
void *osThreadGetArgument(void)
{
    if (osThreadGetId() == 0) {
        return 0;
    }
    return OnGetCurrentTcbUserPara();//根据RTOS特性，返回任务创建时的第二个参数值
}

void OnErrorTest(void)
{
    int i = 1000000;
    while(1)
    {
        HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_RESET);
        i = 1000000;
        while(i--);
        HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_SET);
        i = 1000000;
        while(i--);
    }
}

typedef enum {
    /** No errors. */
    WIFI_SUCCESS = 0,
    /** Invalid parameters */
    ERROR_WIFI_INVALID_ARGS = -1,
    /** Invalid chip */
    ERROR_WIFI_CHIP_INVALID = -2,
    /** Invalid Wi-Fi interface */
    ERROR_WIFI_IFACE_INVALID = -3,
    /** Invalid RTT controller */
    ERROR_WIFI_RTT_CONTROLLER_INVALID = -4,
    /** Wi-Fi not supported by the current version or device */
    ERROR_WIFI_NOT_SUPPORTED = -5,
    /** Wi-Fi unavailable */
    ERROR_WIFI_NOT_AVAILABLE = -6,
    /** Wi-Fi not initialized or started */
    ERROR_WIFI_NOT_STARTED = -7,
    /** System busy */
    ERROR_WIFI_BUSY = -8,
    /** Wi-Fi invalid password */
    ERROR_WIFI_INVALID_PASSWORD = -9,
    /** Unknown error */
    ERROR_WIFI_UNKNOWN = -128
} WifiErrorCode;

WifiErrorCode EnableWifi(void)
{
    return WIFI_SUCCESS;
}

int IsWifiActive(void)
{
    return 1;
}

typedef enum {
    /** A scan based on a specified frequency. */
    WIFI_FREQ_SCAN,
    /** A scan based on a specified SSID. */
    WIFI_SSID_SCAN,
    /** A scan based on a specified BSSID. */
    WIFI_BSSID_SCAN,
    /** A scan based on a specified frequency band. */
    WIFI_BAND_SCAN
} WifiScanType;

#define WIFI_MAX_SSID_LEN 33 // 32 + \0
#define WIFI_MAC_LEN 6
#define WIFI_MAX_KEY_LEN 65 // 64 + \0
#define WIFI_MAX_DNS_NUM 2

typedef struct {
    /** Service set ID (SSID). Its maximum length is defined by {@link WIFI_MAX_SSID_LEN}. */
    char ssid[WIFI_MAX_SSID_LEN];
    /** Length of the SSID. */
    char ssidLen;
    /** Basic service set ID (BSSID). Its length is defined by {@link WIFI_MAC_LEN}. */
    char bssid[WIFI_MAC_LEN];
    /** Frequency. */
    int freqs;
    /** Frequency band. */
    int band;
    /** Wi-Fi scan type, which is defined by {@link WifiScanType}. */
    WifiScanType scanType;
} WifiScanParams;

WifiErrorCode AdvanceScan(WifiScanParams *params)
{
    return WIFI_SUCCESS;
}

WifiErrorCode DisableWifi(void)
{
    return WIFI_SUCCESS;
}


int GetSignalLevel(int rssi, int band)
{
    return 2;
}

typedef struct {
    /** Service set ID (SSID). For its length, see {@link WIFI_MAX_SSID_LEN}. */
    char ssid[WIFI_MAX_SSID_LEN];
    /** Security type */
    int securityType;
    /** Frequency band */
    int band;
    /** Channel number */
    int channelNum;
    /** Key. For its length, see {@link WIFI_MAX_SSID_LEN}. */
    char preSharedKey[WIFI_MAX_KEY_LEN];
} HotspotConfig;

WifiErrorCode SetHotspotConfig(const HotspotConfig* config)
{
    return WIFI_SUCCESS;
}


WifiErrorCode SetBand(int band)
{
    return WIFI_SUCCESS;
}

WifiErrorCode GetBand(int* result)
{
    return WIFI_SUCCESS;
}

int IsHotspotActive(void)
{
    return WIFI_SUCCESS;
}

WifiErrorCode DisableHotspot(void)
{
    return WIFI_SUCCESS;
}

WifiErrorCode EnableHotspot(void)
{
    return WIFI_SUCCESS;
}

typedef struct {
    /** IP address of the Wi-Fi device */
    unsigned int ipAddress;
    /** Gateway of the Wi-Fi device */
    unsigned int gateway;
    /** DNS server addresses for the Wi-Fi device */
    unsigned int dnsServers[WIFI_MAX_DNS_NUM];
    /** Subnet mask of the Wi-Fi device */
    unsigned int netmask;
} IpConfig;

typedef enum {
    /** Static IP address */
    STATIC_IP,
    /** IP address dynamically assigned by DHCP */
    DHCP,
    /** Unknown IP address type */
    UNKNOWN
} IpType;

typedef struct WifiDeviceConfig {
    /** Service set ID (SSID). For its length, see {@link WIFI_MAX_SSID_LEN}. */
    char ssid[WIFI_MAX_SSID_LEN];
    /** Basic service set ID (BSSID). For its length, see {@link WIFI_MAC_LEN}. */
    unsigned char bssid[WIFI_MAC_LEN];
    /** Key. For its length, see {@link WIFI_MAX_KEY_LEN}. */
    char preSharedKey[WIFI_MAX_KEY_LEN];
    /** Security type. It is defined in {@link WifiSecurityType}. */
    int securityType;
    /** Allocated <b>networkId</b> */
    int netId;
    /** Frequency */
    unsigned int freq;
    /** PSK type, see {@link WifiPskType}. */
    int wapiPskType;
    /** IP address type */
    IpType ipType;
    /** Static IP address */
    IpConfig staticIp;
    /* 1 for hidden config */
    int isHiddenSsid;
} WifiDeviceConfig;

WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result)
{
    return WIFI_SUCCESS;
}

WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size)
{
    return WIFI_SUCCESS;
}

typedef struct {
    /** Network name of the station */
    char *name;
    /** MAC address. For its length, see {@link WIFI_MAC_LEN}. */
    unsigned char macAddress[WIFI_MAC_LEN];
    /** IP address of the station */
    unsigned int ipAddress;
    unsigned short disconnectedReason;
} StationInfo;

WifiErrorCode GetStationList(StationInfo* result, unsigned int* size)
{
    return WIFI_SUCCESS;
}

WifiErrorCode GetHotspotConfig(HotspotConfig* result)
{
    return WIFI_SUCCESS;
}

WifiErrorCode RemoveDevice(int networkId)
{
    return WIFI_SUCCESS;
}


WifiErrorCode ConnectTo(int networkId)
{
    return WIFI_SUCCESS;
}

WifiErrorCode GetDeviceMacAddress(unsigned char* result)
{
    return WIFI_SUCCESS;
}

WifiErrorCode Disconnect(void)
{
    return WIFI_SUCCESS;
}

typedef struct {
    /** Service set ID (SSID). For its length, see {@link WIFI_MAX_SSID_LEN}. */
    char ssid[WIFI_MAX_SSID_LEN];
    /** Basic service set ID (BSSID). For its length, see {@link WIFI_MAC_LEN}. */
    unsigned char bssid[WIFI_MAC_LEN];
    /** Security type. For details, see {@link WifiSecurityType}. */
    int securityType;
    /** Received signal strength indicator (RSSI) */
    int rssi;
    /** Frequency band */
    int band;
    /** Frequency in MHz */
    int frequency;
} WifiScanInfo;

WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size)
{
    return WIFI_SUCCESS;
}

typedef enum {
    /** Disconnected */
    WIFI_DISCONNECTED,
    /** Connected */
    WIFI_CONNECTED
} WifiConnState;

typedef struct {
    /** Service set ID (SSID). For its length, see {@link WIFI_MAX_SSID_LEN}. */
    char ssid[WIFI_MAX_SSID_LEN];
    /** Basic service set ID (BSSID). For its length, see {@link WIFI_MAC_LEN}. */
    unsigned char bssid[WIFI_MAC_LEN];
    /** Received signal strength indicator (RSSI) */
    int rssi;
    /** Wi-Fi band information of hotspot */
    int band;
    /** Wi-Fi frequency information of hotspot */
    int frequency;
    /** Wi-Fi connection state, which is defined in {@link WifiConnState} */
    WifiConnState connState;
    /** Reason for Wi-Fi disconnection */
    unsigned short disconnectedReason;
    /** IP address of the connected hotspot */
    unsigned int ipAddress;
} WifiLinkedInfo;

typedef struct {
    /** Connection state change */
    void (*OnWifiConnectionChanged)(int state, WifiLinkedInfo *info);
    /** Scan state change */
    void (*OnWifiScanStateChanged)(int state, int size);
    /** Hotspot state change */
    void (*OnHotspotStateChanged)(int state);
    /** Station connected */
    void (*OnHotspotStaJoin)(StationInfo *info);
    /** Station disconnected */
    void (*OnHotspotStaLeave)(StationInfo *info);
} WifiEvent;

WifiErrorCode RegisterWifiEvent(WifiEvent* event)
{
    return WIFI_SUCCESS;
}

WifiErrorCode UnRegisterWifiEvent(const WifiEvent* event)
{
    return WIFI_SUCCESS;
}

WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result)
{
    return WIFI_SUCCESS;
}

WifiErrorCode Scan(void)
{
    return WIFI_SUCCESS;
}